<script setup>
import {ref} from 'vue'
import useTable from '@/utils/useTable'
import {http} from '@/api'
import { ElMessage } from 'element-plus'
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openDelete
} = useTable({
  requestFn: http.system,
  params: {
    name: '',
  }
})
const show = ref(false)
const formRef4 = ref(null)
const formData4 = ref({
  id: undefined,
  name: '',
  value: '',
})
const rules4 = ref({
  name: [
    {required: true, message: '请输入', trigger: 'blur'},
  ],
})
const openAdd = () => {
  formData4.value = {
    id: undefined,
    name: '',
    image_url: '',
    status: 0,
  }
  show.value = true
}
const openEdit = (row) => {
  formData4.value = {
    id: row.id,
    name: row.name,
    image_url: row.image_url,
    status: row.status,
  }
  show.value = true
}
const submit4 = () => {
  formRef4.value.validate((valid) => {
    if (valid) {
      console.log('submit!')
      if (formData4.value.id) {
        http.system.update(formData4.value).then(res => {
          ElMessage.success(res.msg)
          show.value = false
          search()
        })
      } else {
        http.system.create(formData4.value).then(res => {
          ElMessage.success(res.msg)
          show.value = false
          search()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const reset4 = () => {
  show.value = false
}

</script>

<template>
  <PageMain title="系统图数据">
    <el-form label-suffix="：" inline>
      <el-form-item label="名称">
        <el-input v-model="searchParams.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="图片">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="image" :image-url="scope.row.image_url"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="状态" prop="status"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>

  <el-dialog v-model="show" title="新增/编辑" width="500">
    <el-form ref="formRef4" :model="formData4" :rules="rules4" label-width="60px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData4.name" ></el-input>
      </el-form-item>
      <el-form-item label="图片" style="width: 100%" prop="image">
        <UploadImage v-model:image-url="formData4.image_url"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="formData4.status" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset4">取消</el-button>
        <el-button type="primary" @click="submit4">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>